import './public-path';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { getI18n } from 'react-i18next';
import { message } from 'antd';
import './index.css';
import './lang';
import App from './App';

export let microProps;

function render(props) {
  const { base, container } = props;
  const root = container ? container.querySelector('#root') : document.querySelector('#root');
  message.config({
    getContainer: () => root,
  });
  ReactDOM.render(
    <BrowserRouter basename={window.__POWERED_BY_QIANKUN__ ? base : '/'}>
      <App {...props} />
    </BrowserRouter>,
    root
  );
}

if (!window.__POWERED_BY_QIANKUN__) {
  render({});
}

export async function bootstrap() {
  console.log('子应用启动');
}

export async function mount(props) {
  console.log('挂载子应用', props);
  getI18n().changeLanguage(props.tyLang);
  microProps = props;
  render(props);
}

export async function unmount(props) {
  const { container } = props;
  message.destroy();
  const root = container ? container.querySelector('#root') : document.querySelector('#root');

  ReactDOM.unmountComponentAtNode(root);
}
